package com.course.dao;

import java.util.List;
import java.util.Map;

import com.course.domain.Course;
import com.course.domain.SCId;
import com.course.domain.StuCour;
import com.course.domain.Student;
import com.course.hibernate.HibernateSessionFactory;

public class StuCourDao extends SupperDao
{

	@SuppressWarnings( "unchecked" )
	public List< StuCour > getStudentCoursByStudentId( Integer id )
	{
		String hql = "SELECT c.name, t.name, sc.score, c.finish FROM Course c, StuCour sc, Teacher t WHERE sc.id.course.id = c.id AND c.teacher.id = t.id AND sc.id.student.id = " + id;
		return this.findByHQL( hql );
	}

	@SuppressWarnings( "unchecked" )
	public List< StuCour > getNoChooseCourse( Integer id )
	{
		String hql = "from Course c, Teacher t, Student s where c.chooseStatus = true and c.myClass.id = s.myClass.id and s.myClass.finish = false and s.id = " + id +" and c.teacher.id = t.id and c.id not in ( select sc.id.course.id from StuCour sc where sc.id.student.id = " +String.valueOf( id ) +  " )";
		return this.findByHQL( hql );
	}

	public boolean saveStuCour( Integer id, String[] course )
	{
		SCId scid = null;
		StuCour sc = null;
		
		try
		{
			session = HibernateSessionFactory.getSession();// 获取Session对象
			session.beginTransaction(); // 开启事物
			
			for ( String cour : course )
			{
				scid = new SCId();
				Student stu = new Student();
				Course cr = new Course();
				stu.setId( id );
				cr.setId( Integer.valueOf( cour ) );
				scid.setCourse( cr );
				scid.setStudent( stu );
				sc = new StuCour();
				sc.setId( scid );
				session.save( sc );
			}
			session.getTransaction().commit(); // 提交事物
			session.close();
		} catch ( Exception e )
		{
			e.printStackTrace(); // 打印异常信息
			session.getTransaction().rollback(); // 回滚事物
			session.close();
			return false;
		}
		return true;
	}

	@SuppressWarnings( "unchecked" )
	public List< Object[] > getStuCoursByCourseId( int courseId )
	{
		String hql = "select s.id, s.username, s.name, mc.name, sc.score from Course c, StuCour sc, Student s, MyClass mc where sc.id.course.id = c.id and sc.id.student.id = s.id and s.myClass.id = mc.id and c.id = " + courseId;
		return this.findByHQL( hql );
	}

	public boolean uploadStuCour( List< Object[] > list )
	{
		SCId scid = null;
		StuCour sc = null;
		try
		{
			session = HibernateSessionFactory.getSession(); // 获取Session对象
			session.beginTransaction(); // 开启事物
			for ( Object[] obj : list )
			{
				scid = new SCId();
				Student stu = new Student();
				Course cr = new Course();
				stu.setId( ( Integer )obj[1] );
				cr.setId( ( Integer )obj[0] );
				scid.setCourse( cr );
				scid.setStudent( stu );
				sc = new StuCour();
				sc.setId( scid );
				sc.setScore( ( Double )obj[2] );
				session.update( sc );
			}
			session.getTransaction().commit(); // 提交事物
			session.close();
		} catch ( Exception e )
		{
			e.printStackTrace(); // 打印异常信息
			session.getTransaction().rollback(); // 回滚事物
			session.close();
			return false;
		}
		return true;
	}

	public boolean updateScore( int courseId, int studentId, double score )
	{
		SCId id = new SCId();
		id.setCourse( ( Course )this.get( Course.class, courseId ) );
		id.setStudent( ( Student )this.get( Student.class, studentId ) );
		
		StuCour stuCour = ( StuCour )this.get( StuCour.class, id );
		stuCour.setScore( score );
		return this.update( stuCour );
		
	}

	@SuppressWarnings( "unchecked" )
	public long countStuCour( int courseId )
	{
		String hql = "select count(*) from StuCour sc where sc.id.course.id = " + courseId;
		List<Long> list = this.findByHQL( hql );
		return list.get( 0 );
	}

	@SuppressWarnings( "unchecked" )
	public List< Object[] > getStuCoursPageByCourseId( int courseId, int start, int length )
	{
		String hql = "select s.id, s.username, s.name, mc.name, sc.score from Course c, StuCour sc, Student s, MyClass mc where sc.id.course.id = c.id and sc.id.student.id = s.id and s.myClass.id = mc.id and c.id = " + courseId;
		return this.findPaging( hql, start, length, null );
	}

	public boolean updateScores( int courseId, String[] students, Map< Integer, Double > score )
	{
		SCId scid = null;
		StuCour sc = null;
		Course cr = new Course();
		cr.setId( courseId );
		try
		{
			session = HibernateSessionFactory.getSession(); // 获取Session对象
			session.beginTransaction(); // 开启事物
			for ( String studentId : students )
			{
				scid = new SCId();
				Student stu = new Student();
				stu.setId( Integer.parseInt( studentId ) );
				
				scid.setCourse( cr );
				scid.setStudent( stu );
				sc = new StuCour();
				sc.setId( scid );
				sc.setScore( score.get( Integer.parseInt( studentId ) ) );
				session.update( sc );
			}
			session.getTransaction().commit(); // 提交事物
			session.close();
		} catch ( Exception e )
		{
			e.printStackTrace(); // 打印异常信息
			session.getTransaction().rollback(); // 回滚事物
			session.close();
			return false;
		}
		return true;
	}

}
